var BSTYUN = BSTYUN || {};
BSTYUN.invoiceDemand = {
		init: function(){
			var that = this;
			// 切换个人和企业
			that.switchInvoiceTab();
			// 上传图片
			that.uploaderImg();
		},
		switchInvoiceTab: function(){
			var $openType = $(".openType"),
				$J_personal = $(".J_personal") ,
				$J_company  = $(".J_company") ;
			var $inputs = $("#J_form input") ;
			var $radio_openType = $("input[name=openType]");
			var $invoiceType = $("input[name=invoiceType]"),
				$J_zhuanpiao  = $(".J_zhuanpiao ");
			var $invoiceTitle = $inputs.filter("[name=invoiceTitle]") , 
				$invoiceType= $inputs.filter("[name=invoiceType]") ,
				$taxNum = $inputs.filter("[name=taxNum]") ,
				$basBankName = $inputs.filter("[name=basBankName]") ,
				$basAccountCode = $inputs.filter("[name=basAccountCode]") ,
				$registerAddress = $inputs.filter("[name=registerAddress]") ,
				$registerPhone = $inputs.filter("[name=registerPhone]") ,
				$bnessLicensePhoto = $("#bnessLicensePhoto") ,
				$taxCopyPhoto = $("#taxCopyPhoto") ,
				$taxpayerCheckPhoto = $("taxpayerCheckPhoto") ;
			$openType.on("click","input[name=openType]",function(){
				var $this = $(this);
				if( $this.val() == "1" ){
					$J_personal.hide();
					$J_company.show();
				}else {
					$J_personal.show();
					$J_company.hide();
					$J_zhuanpiao.hide();
					$invoiceType.eq(0).prop("checked",true)
				}
			});
			$invoiceType.on("click",function(){
				var $this = $(this);
				if( $this.val() == "1" ){
					$J_zhuanpiao.show();
				}else {
					$J_zhuanpiao.hide();
				}
			});
			// 提交
			$submit = $("#submit");
			// 获取数据
			function getData(){
				if( $radio_openType.prop("checked") ){
					// 开具类型：个人
					return {
						openType: 0 ,
						invoiceTitle: "个人" ,
						invoiceType: 0 ,
					};
				}else{
					// 开具类型：企业
					if( $invoiceType.prop("checked") ){
						return {
							openType: 1 ,
							invoiceTitle: $.trim( $invoiceTitle.val() ),
							invoiceType: 0 ,
						};
					}else{
						return {
							openType: 1 ,
							invoiceTitle: $.trim( $invoiceTitle.val() ) ,
							invoiceType: 1 ,
							taxNum: $.trim( $taxNum.val() ),
							basBankName: $.trim( $basBankName.val() ),
							basAccountCode: $.trim( $basAccountCode.val() ) ,
							registerAddress: $.trim( $registerAddress.val() ) ,
							registerPhone: $.trim( $registerPhone.val() ) ,
							bnessLicensePhoto: $.trim( $("#bnessLicensePhoto").data("url") ) ,
							taxCopyPhoto: $.trim( $("#taxCopyPhoto").data("url") ) ,
							taxpayerCheckPhoto: $.trim( $("#taxpayerCheckPhoto").data("url") )  
						};
					};
				};
			};
			function checkData( data ){
				if( data.openType  == 0 ){
					return data;
				}else{
					if( data.invoiceTitle == "" ){
						BSTYUN.dialog.warn("发票抬头不能为空！");
						return false;
					};
					if( data.invoiceType == 0 ){
						return data;
					}else{
						if( data.taxNum == "" ){
							BSTYUN.dialog.warn("税务登记证号不能为空！");
							return false;
						};
						if( data.basBankName == "" ){
							BSTYUN.dialog.warn("基本开户银行名称不能为空！");
							return false;
						};
						if( data.basAccountCode == "" ){
							BSTYUN.dialog.warn("基本户开户账号不能为空！");
							return false;
						};
						if( data.registerAddress == "" ){
							BSTYUN.dialog.warn("注册场所地址不能为空！");
							return false;
						};
						if( data.registerPhone == "" ){
							BSTYUN.dialog.warn("注册固定电话不能为空！");
							return false;
						};
						if( !/^0\d{2,3}-\d{5,9}$/.test( data.registerPhone ) ){
							BSTYUN.dialog.warn("注册固定电话格式不正确！");
							return false;
						};
						if( data.bnessLicensePhoto == "" ){
							BSTYUN.dialog.warn("请上传营业执照复印件！");
							return false;
						};
						if( data.taxCopyPhoto == "" ){
							BSTYUN.dialog.warn("请上传税务登记复印件！");
							return false;
						};
						if( data.taxpayerCheckPhoto == "" ){
							BSTYUN.dialog.warn("请上传一般纳税人资格认证复印件！");
							return false;
						};
						return data;
					};
				}
			};
			$submit.on("click",function(){
				var data = getData();
				if( checkData( data ) ){
					
					$.ajax({
						type: 'post' ,
						url: '/finance/invoice/addInvoiceModel' ,
						data: data,
						dataType: 'json' ,
						success: function(){
							location.href="/finance/invoice/invoiceDemandDetail";
						},
						error: function(){
							
						}
					});
				};
			});
		},
		uploaderImg: function(){
			var $uploder = $(".uploder") ;
			// 图片上传
			$uploder.on("change",".file-input",function(){
				var $this = $( this ),
					id= $this.attr("id");
				// 检查图片格式
				var f = $this.val();
				if(!/.(gif|bmp|jpg|jpeg|png|JPG|PNG)$/.test(f)) {
				    BSTYUN.dialog.warn("图片类型必须是gif,jpeg,jpg,png,bmp中的一种!");
				    return false;
				};
				$.ajaxFileUpload({
					url:"/uploadFile/image?attachType=1&objectName=invoice",
					secureuri:false,
					fileElementId: $this.attr("id") ,
					dataType:"json",
					success:function(data) {
						if(data.message == "上传文件成功") {
							$("#"+id).data("url", data.path );
							BSTYUN.dialog.success("上传文件成功");
						}else{
							BSTYUN.dialog.warn(data.message);
						}
					},
					error:function (data, status, e) {
						BSTYUN.dialog.warn("图片上传失败,请重新选择图片！");
					}
				});
			});
			
		}
};
$(function(){
	BSTYUN.invoiceDemand.init();
});